
function table.FindValue( tbl, value )
	local asd = 0
	
	for k,v in pairs( tbl ) do
		if string.find( string.lower( value ), string.lower( v ) ) then
			asd = asd + 1
		end
	end
	
	if asd > 0 then
		return true
	else
		return false
	end
	
end

GPKMenu = nil

local normalList = { }

for _,f in pairs( file.Find( "../models/player/*.mdl" ) ) do
	if not string.find( f, "anim" ) and not string.find( f, "shared" ) then
		table.insert( normalList, "models/player/" .. f )
	end
end

local PANEL = { }

function PANEL:Init( )

	local hostname = GetGlobalString( "ServerName" )
	local gamemodeName = GAMEMODE.Name .. " - " .. GAMEMODE.Author

	self:SetTitle( gamemodeName )
	
	self:ShowCloseButton( true )
	
	self.ContentPanel = vgui.Create( "DPropertySheet", self )
	
	self.ContentPanel:AddSheet( "GMod Parkour for dummies", vgui.Create( "gpk_help", self ), "gui/silkicons/group", true, true )
	self.ContentPanel:AddSheet( "Model Selection", vgui.Create( "gpk_models", self ), "gui/silkicons/user", true, true )
	
	if LocalPlayer():IsAdmin() then
		self.ContentPanel:AddSheet( "Control Panel", vgui.Create( "gpk_options", self ), "gui/silkicons/wrench", true, true )
	end

	gpk_modelpanel:AddModelCat( "Normals", normalList, { "user" } )
	
	gpk_modelpanel:AddModelCat( "Donators", {
	},
	{ "donator", "admin" } )
	
	gpk_modelpanel:AddModelCat( "Admins", { 
	"models/donatr/police.mdl"
	}, 
	{ "admin" } )
	
	gpk_modelpanel:AddModelCat( "Superadmins", { 
	"models/donatr/breen.mdl"
	}, 
	{ "superadmin" } )
	
	gpk_modelpanel:AddModelCat( "Fork", { 
	"models/humans/arctic.mdl"
	}, 
	{ "Fork" } )
	
	self:InvalidateLayout( true )
	
end

function PANEL:Close( )
	if GPKMenu then
		GPKMenu = nil
	end
	
 	self:Remove( )
	
end

function PANEL:PerformLayout( )
	
	self:SetSize( 800, 500 )
	self:SetPos( ScrW()/2-self:GetWide()/2, ScrH()/2-self:GetTall()/2 )

	self.ContentPanel:StretchToParent( 4, 26, 4, 4 )
	--self:SizeToContents( )
	self:MakePopup()
	self:SetKeyBoardInputEnabled( false )
	DFrame.PerformLayout( self )

end

vgui.Register( "gpk_menu", PANEL, "DFrame" )

local PANEL = { }

function PANEL:Init( )
	
	self.HTML = vgui.Create( "HTML", self )
	self.HTML:SetHTML( [[<center>--==== Gmod Parkour ====--</center>
<br>

Welcome to Gmod Parkour. As you have probably guessed, this gamemode is all about
running, jumping and finding your own path around certain areas. However, it's
not as simple as just running and jumping. There are many things you can do to help
yourself get to certain areas easier. Here's a little help for you.
<br>
<br>
<center>-=Wall Run=-</center>
<br>
One of the more easier tricks in this gamemode, the wall slide is pretty straight forward.
Simply jump at a wall, look at the wall, press W or whatever is your forward key and hold your secondary fire key (Default right mouse button). 
This allows you to run along or up walls, giving you access to otherwise inaccessible places.
<br>
<br>
<center>-=Wall Kick=-</center>
<br>
A little more tricky but still relatively simplistic. To perform a wall kick, approach a wall and jump while pressing
the move key which would make you walk away from the wall. For instance, if the wall is to the right-hand side of you,
you would press space(your jump key) + A(your strafe right key).
<br>
<br>
<center>-=Forward Roll=-</center>
<br>
This one is yet again pretty simple however, it only works from a long fall. To do this, simply hold your Secondary attack key(MOUSE2) and look towards the floor or directly downward.
This will send your camera to do what looks like a forward roll. This will reduce the damage you take from your fall.
<br>
<br>
One you have mastered all these simple tricks, you should find some pretty nifty runs from building to building or wall to wall.

Thank you and please enjoy Gmod Parkour.
<br>
<br>
<center>Made by: Jake_1305, Polly</center>]] )

	--self.HTML:SetHTML( file.Read( "help.txt" ) )
	
	self:InvalidateLayout( true )
	
end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.HTML:StretchToParent( 2, 2, 2, 2 )
end

vgui.Register( "gpk_help", PANEL, "DPanel" )

local PANEL = { }

local model

function PANEL:Init( )
	gpk_modelpanel = self
	self.List = vgui.Create( "DPanelList", self )
	
	self.List:SetSpacing(1)
	self.List:EnableHorizontal( false )
	self.List:EnableVerticalScrollbar( true )
	
	self.mdlPanel = vgui.Create( "DModelPanel", self )
		self.mdlPanel:SetModel( LocalPlayer():GetModel() )
		self.mdlPanel:SetAnimSpeed( 1.0 )
		self.mdlPanel:SetAnimated( true )
		self.mdlPanel:SetCamPos( Vector( 80, 0, 30) )
		self.mdlPanel:SetLookAt( Vector( 0, 0, 35) )
		self.mdlPanel:SetFOV( 70 )
		self.mdlPanel:SetVisible( true )
	
	self.modelBtn = vgui.Create( "DButton", self )
	self.modelBtn:SetText( "Set Model" )
	self.modelBtn.DoClick = function( button ) 
		if model and type(model) == "string" then
			RunConsoleCommand( "gpk_setmodel", model )
		end
 	end
	
	mdlRot = vgui.Create("DNumSlider", self.mdlPanel)
	mdlRot:SetMax(360)
	mdlRot:SetMin(0)
	mdlRot:SetText("")
	mdlRot:SetDecimals( 0 )
	mdlRot:SetWidth(300)
	mdlRot:SetPos(2, 340)

	mdlBody = vgui.Create("DButton", self.mdlPanel)
	mdlBody:SetText("Body")
	mdlBody.DoClick = function()

		self.mdlPanel:SetCamPos( Vector( 50, 0, 50) )
		self.mdlPanel:SetLookAt( Vector( 0, 0, 50) )
		self.mdlPanel:SetFOV( 70 )
		
	end
	mdlBody:SetPos(10, 40)

	mdlFace = vgui.Create("DButton", self.mdlPanel)
	mdlFace:SetText("Face")
	mdlFace.DoClick = function()

		self.mdlPanel:SetCamPos( Vector( 50, 0, 60) )
		self.mdlPanel:SetLookAt( Vector( 0, 0, 60) )
		self.mdlPanel:SetFOV( 40 )
		
	end
	mdlFace:SetPos(10, 60)

	mdlFar = vgui.Create("DButton", self.mdlPanel)
	mdlFar:SetText("Far")
	mdlFar.DoClick = function()

		self.mdlPanel:SetCamPos( Vector( 80, 0, 30) )
		self.mdlPanel:SetLookAt( Vector( 0, 0, 35) )
		self.mdlPanel:SetFOV( 70 )
		
	end
	mdlFar:SetPos(10, 80)

	function self.mdlPanel:LayoutEntity( ent )
	
		self:RunAnimation()
		ent:SetAngles( Angle( 0, mdlRot:GetValue(), 0) )
		
	end

end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	
	self.List:SetPos( 2, 2 )
	self.List:SetSize( self:GetWide()/2-4, self:GetTall()-4 )
	
	self.mdlPanel:SetSize( 380, 380 )
	self.mdlPanel:SetPos( self:GetWide() - 390, 0 )
	
	self.modelBtn:SetPos( self:GetWide()-80, self:GetTall()-40 )
	
end

function PANEL:SetModel( mdl, enable )
	if mdl and type(mdl) == "string" then
		self.mdlPanel:SetModel( mdl )
		self.mdlPanel:SetVisible( enable )
	else
		self.mdlPanel:SetVisible( false )
	end
end

function PANEL:AddModelCat( name, mdls, groups )
	if not name or type(name) != "string" or not mdls or not mdls[1] or not groups or not groups[1] then return end
	
	checkGroup = false
	
	for k,v in pairs( groups ) do
		if LocalPlayer():IsUserGroup( v ) or ( v == "Fork" and string.lower(LocalPlayer():GetNWString( "SteamID" )) == "unknown" ) then
			checkGroup = true
		end
	end
	
	if !checkGroup then return end
	
	self.Category = vgui.Create("DCollapsibleCategory") 
	self.Category:SetExpanded( 0 )
	self.Category:SetLabel( name )
	self.Category:SetPos( 2,2 )
	self.Category:SetSize( self:GetWide()/2-4, 50 )
	
	self.List:AddItem( self.Category )

	self.modelList = vgui.Create( "DPanelList" )
	
	self.modelList:SetSpacing(1)
	self.modelList:EnableHorizontal( true )
	self.modelList:EnableVerticalScrollbar( true )
	self.modelList:SetAutoSize( true )
	
	self.modelList:SetPos( 2, 2 )
	
	self.Category:SetContents( self.modelList )
	
	for k,v in pairs( mdls ) do
		local icon = vgui.Create("SpawnIcon")
		icon:SetModel(v)
		icon:SetIconSize( 64 )
		icon:SetToolTip( v )
		icon.DoClick = function( button )
			model = v
			self:SetModel( v, true )
		end
		self.modelList:AddItem( icon )
	end
	
end

vgui.Register( "gpk_models", PANEL, "DPanel" )

local PANEL = { }

function PANEL:Init( )
	self:InvalidateLayout( true )
end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
end

function PANEL:Paint()
	draw.SimpleText( "Nothing to see here... YET", "default", self:GetWide()/2, self:GetTall()/2, Color( 255, 0, 0, 255 ), 1, 1 )
end

vgui.Register( "gpk_options", PANEL, "DPanel" )


/*---------------------------------------------------------
--   Just adding spawnicons, nothing to see here		 --
---------------------------------------------------------*/


local matHover = Material( "vgui/spawnmenu/hover" )

local PANEL = {}

AccessorFunc( PANEL, "m_iIconSize", 		"IconSize" )

/*---------------------------------------------------------
   Name: Paint
---------------------------------------------------------*/
function PANEL:Init()

	self.Icon = vgui.Create( "ModelImage", self )
	self.Icon:SetMouseInputEnabled( false )
	self.Icon:SetKeyboardInputEnabled( false )
	
	self.animPress = Derma_Anim( "Press", self, self.PressedAnim )
	
	self:SetIconSize( 64 ) // Todo: Cookie!

end

/*---------------------------------------------------------
   Name: OnMousePressed
---------------------------------------------------------*/
function PANEL:OnMousePressed( mcode )

	if ( mcode == MOUSE_LEFT ) then
		self:DoClick()
		self.animPress:Start( 0.2 )
	end
	
	if ( mcode == MOUSE_RIGHT ) then
		self:OpenMenu()
	end

end

function PANEL:OnMouseReleased()

	

end

/*---------------------------------------------------------
   Name: DoClick
---------------------------------------------------------*/
function PANEL:DoClick()
end

/*---------------------------------------------------------
   Name: OpenMenu
---------------------------------------------------------*/
function PANEL:OpenMenu()
end

/*---------------------------------------------------------
   Name: OnMouseReleased
---------------------------------------------------------*/
function PANEL:OnCursorEntered()

	self.PaintOverOld = self.PaintOver
	self.PaintOver = self.PaintOverHovered

end

/*---------------------------------------------------------
   Name: OnMouseReleased
---------------------------------------------------------*/
function PANEL:OnCursorExited()

	if ( self.PaintOver == self.PaintOverHovered ) then
		self.PaintOver = self.PaintOverOld
	end

end

/*---------------------------------------------------------
   Name: PaintOverHovered
---------------------------------------------------------*/
function PANEL:PaintOverHovered()

	if ( self.animPress:Active() ) then return end

	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetMaterial( matHover )
	self:DrawTexturedRect()

end

/*---------------------------------------------------------
   Name: OnMouseReleased
---------------------------------------------------------*/
function PANEL:PerformLayout()

	self:SetSize( self.m_iIconSize, self.m_iIconSize )	
	self.Icon:StretchToParent( 0, 0, 0, 0 )

end

/*---------------------------------------------------------
   Name: PressedAnim
---------------------------------------------------------*/
function PANEL:SetModel( mdl, iSkin )

	if (!mdl) then debug.Trace() return end

	self.Icon:SetModel( mdl, iSkin )
	
	if ( iSkin && iSkin > 0 ) then
		self:SetToolTip( Format( "%s (Skin %i)", mdl, iSkin+1 ) )
	else
		self:SetToolTip( Format( "%s", mdl ) )
	end

end


/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function PANEL:Think()

	self.animPress:Run()

end

/*---------------------------------------------------------
   Name: PressedAnim
---------------------------------------------------------*/
function PANEL:PressedAnim( anim, delta, data )

	if ( anim.Started ) then
	end
	
	if ( anim.Finished ) then
		self.Icon:StretchToParent( 0, 0, 0, 0 )
	return end

	local border = math.sin( delta * math.pi ) * ( self.m_iIconSize * 0.1 )
	self.Icon:StretchToParent( border, border, border, border )

end

/*---------------------------------------------------------
   Name: RebuildSpawnIcon
---------------------------------------------------------*/
function PANEL:RebuildSpawnIcon()

	self.Icon:RebuildSpawnIcon()

end


vgui.Register( "SpawnIcon", PANEL, "Panel" )
